﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;

using System.Text;

using System.Data.SqlClient;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Collections;

public partial class TicketReturn_Managerment : System.Web.UI.Page
{
    private string strconn;
    SqlCommand comm;
    SqlConnection conn;
    DataSet ds;
    SqlDataAdapter da;

    protected void Page_Load(object sender, EventArgs e)
    {
        strconn = "Server = localhost;database=dbTrain;trusted_connection=true";
        conn = new SqlConnection(strconn);
        ds = new DataSet();
        if (!IsPostBack)
            bindproducttogridview();
    }
    protected void bindproducttogridview()
    {
        try
        {
            if (conn.State == ConnectionState.Closed)
                conn.Open();
            comm = new SqlCommand("viTicketReturn", conn);
            comm.CommandType = CommandType.StoredProcedure;
            da = new SqlDataAdapter(comm);
            da.Fill(ds);
            GridView1.DataSource = ds.Tables[0];
            GridView1.DataBind();
        }
        catch (SqlException ex)
        {
            throw new Exception(ex.Message);
        }
        finally
        {
            conn.Close();
        }
    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        try
        {
            if (conn.State == ConnectionState.Open)
                conn.Close();
            else
                conn.Open();
            comm = new SqlCommand("AddTicketReturn", conn);
            comm.CommandType = CommandType.StoredProcedure;
            
            comm.Parameters.Add("@TicketName", SqlDbType.NVarChar).Value = txtName.Text;
            comm.Parameters.Add("@Description", SqlDbType.NVarChar).Value = txtDescription.Text; ;
            comm.Parameters.Add("@PercentPrice", SqlDbType.Int).Value = txtPrice.Text;
            comm.Parameters.Add("@DateReturn", SqlDbType.Int).Value = txtDate.Text;
            comm.ExecuteNonQuery();
            bindproducttogridview();
        }
        catch (SqlException ex)
        {
            throw new Exception(ex.Message);
        }
        finally
        {
            conn.Close();
        }
    }
    protected void GridView1_RowDeleting(object sender, System.Web.UI.WebControls.GridViewDeleteEventArgs e)
    {
        try
        {
            ds = new DataSet();
            if (conn.State == ConnectionState.Closed)
                conn.Open();

            GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
            TicketReturnID = int.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());

            comm = new SqlCommand("DeleteTicketReturn", conn);
            comm.CommandType = CommandType.StoredProcedure;
            comm.Parameters.Add("TicketReturnID", SqlDbType.Int).Value = TicketReturnID;
           
            da = new SqlDataAdapter(comm);
            comm.ExecuteNonQuery();
            bindproducttogridview();

        }
        catch (SqlException ex)
        {
            throw new Exception(ex.Message);
        }
        finally
        {
            conn.Close();
        }
    }
    protected void GridView1_RowEditing(object sender, System.Web.UI.WebControls.GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        bindproducttogridview();
    }

    
    protected void GridView1_RowCancelingEdit(object sender, System.Web.UI.WebControls.GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        bindproducttogridview();
    }
    protected void GridView1_RowUpdating(object sender, System.Web.UI.WebControls.GridViewUpdateEventArgs e)
    {
        if (conn.State == ConnectionState.Open)
            conn.Close();
        else
            conn.Open();

        comm = new SqlCommand("UpdateTicketReturn", conn);
        comm.CommandType = CommandType.StoredProcedure;

        int TicketReturnID = Convert.ToInt32(((TextBox)GridView1.Rows[e.RowIndex].Cells[0].Controls[0]).Text);
        string TicketName = ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
        string Description = ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
        int PercentPrice = Convert.ToInt32(((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text);
        int DateReturn = Convert.ToInt32(((TextBox)GridView1.Rows[e.RowIndex].Cells[4].Controls[0]).Text);
        
        
        
        comm.Parameters.Add("@TicketReturnID", SqlDbType.Int).Value = TicketReturnID;
        comm.Parameters.Add("@TicketName", SqlDbType.NVarChar).Value = TicketName;
        comm.Parameters.Add("@Description", SqlDbType.NVarChar).Value = Description;
        comm.Parameters.Add("@PercentPrice", SqlDbType.Int).Value = PercentPrice;
        comm.Parameters.Add("@DateReturn", SqlDbType.Int).Value = DateReturn;

        
        comm.ExecuteNonQuery();
        //bindproducttogridview();

        GridView1.EditIndex = -1;
        bindproducttogridview();
        conn.Close();
        
    }
    protected void Button1_Click(object sender, EventArgs e)
    {

    }

    private int ticketReturnID;

    public int TicketReturnID
    {
        get { return ticketReturnID; }
        set { ticketReturnID = value; }
    }
}